<?php
class Marketing_AdminItemController extends Zendvn_Controller_Action{

	protected $_arrParam; //Mang tham so nhan duoc khi mot Action chay
	protected $_currentController; //Duong dan cua Controller
	protected $_actionMain; //Duong dan cua Action chinh
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 15,
									'pageRange' => 10,
									'currentPage' => 1
									);
	protected $_namespace;
	protected $_page = ''; //Url page
	
	public function init(){
		$this->_arrParam = $this->_request->getParams(); //Mang tham so nhan duoc khi mot Action chay
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller']; //Duong dan cua Controller
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index'; //Duong dan cua Action chinh
		$this->_controllerMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller']; //Duong dan cua Controller chinh
	
		//Luu cac du lieu filter vaof SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//Lay thong tin so phan tu tren mot trang
		if(isset($this->_arrParam['limitPage'])){
			$ssFilter->limitPage = $this->_request->getParam('limitPage');
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}elseif(!empty($ssFilter->limitPage)){
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}
		
		//Trang hien tai
		if(isset($this->_arrParam['page'])){
			$this->_paginator['currentPage'] = $this->_arrParam['page'];
			$this->_page = '/page/' . $this->_arrParam['page'];
		}

		$this->_arrParam['paginator'] = $this->_paginator; //Truyen thong tin phan trang vao mang du lieu
		
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
			$ssFilter->col 			= 'mi.id';
			$ssFilter->order 		= 'DESC';
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		$this->_arrParam['ssFilter']['col'] 		= $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 		= $ssFilter->order;
		
		//Thanh vien dang login
		$info 			= new Zendvn_System_Info();
		$this->_arrParam['adminInfo'] 	= $info->getMemberInfo();
		
		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$this->view->siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/admin/" . $this->view->siteConfig['template']['admin'];
		$this->loadTemplate($template_path, 'template.ini', 'daotao');
	}

	public function indexAction(){
		$this->view->Title = 'Marketing :: Danh sách';
		$this->view->headTitle($this->view->Title, true);
		
		$tblItem 			= new Marketing_Model_Item();
		$tblCategory	= new Marketing_Model_Category();
		
		$this->view->Category	= $tblCategory->itemInSelectbox();
		$this->view->Items 	= $tblItem->listItem($this->_arrParam, array('task'=>'admin-list'));
		$totalItem 			= $tblItem->countItem($this->_arrParam, array('task'=>'admin-list'));
		
		$paginator 			= new Zendvn_Paginator();
		$this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
	}
	
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		if($this->_arrParam['type'] == 'search'){
			if($this->_arrParam['key'] == 1){
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
			}else{
				$ssFilter->keywords = '';
			}
		}
	
		if($this->_arrParam['type'] == 'order'){
			$ssFilter->col = $this->_arrParam['col'];
			$ssFilter->order = $this->_arrParam['by'];
		}
		
		$this->_redirect($this->_actionMain);
	
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function addAction(){
		$this->view->Title = 'Marketing :: Thêm mới';
		$this->view->headTitle($this->view->Title, true);
		
		$tblItem 		= new Marketing_Model_Item();
		$tblCategory	= new Marketing_Model_Category();
		$tblUser		= new Default_Model_Users();
		$tblKhoahoc		= new Daotao_Model_Khoahoc();
		
		$this->view->Khoahoc	= $tblKhoahoc->itemInSelectbox(null, array('task' => 'get-khoahoc'));
		$this->view->Users		= $tblUser->listItem(null,array('task' => 'user-all'));
		$this->view->Category	= $tblCategory->itemInSelectbox();
		
		if($this->_request->isPost()){
			$validator = new Marketing_Form_ValidateItem($this->_arrParam);
			if($validator->isError() == true){
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			}else{
				$arrParam 	= $validator->getData();
				$lastID 	= $tblItem->saveItem($arrParam,array('task'=>'admin-add'));
				
				if(!empty($this->_arrParam['cid'])) {
					$this->view->User = $tblItem->listItem($this->_arrParam, array('task' => 'admin-user'));
					$this->addXmlAction($lastID, $this->view->User);
				}
				
				if(empty($this->_arrParam['type'])){
					$this->_redirect($this->_actionMain . '/save/ok');
				}else{
					if($this->_arrParam['type'] == 'apply'){
						$this->_redirect($this->_controllerMain . '/edit/id/' . $lastID);
					}
					if($this->_arrParam['type'] == 'new'){
						$this->_redirect($this->_controllerMain . '/add');
					}
				}
			}
		}
		
	}
	
	public function infoAction(){
		$this->view->Title = 'Marketing :: Xem thông tin';
		$this->view->headTitle($this->view->Title, true);
		
		$tblItem 		= new Marketing_Model_Item();
		$tblKhoahoc		= new Daotao_Model_Khoahoc();
		$tblUser		= new Default_Model_Users();
		
		$this->view->Khoahoc	= $tblKhoahoc->itemInSelectbox(null, array('task' => 'get-khoahoc'));
		$this->view->Users		= $tblUser->listItem(null,array('task' => 'user-all'));
		$this->view->Item 		= $tblItem->getItem($this->_arrParam,array('task'=>'admin-info'));
		
		$tblUser = new Default_Model_Users();
		if(!empty($this->view->Item['created_by'])){
			$this->view->createdBy = $tblUser->getItem(array('id' => $this->view->Item['created_by']),array('task'=>'admin-info'));
		}
		if(!empty($this->view->Item['modified_by'])){
			$this->view->modifiedBy = $tblUser->getItem(array('id' => $this->view->Item['modified_by']),array('task'=>'admin-info'));
		}
	}
	
	public function editAction(){
		$this->view->Title = 'Marketing :: Sửa';
		$this->view->headTitle($this->view->Title, true);
		
		$tblItem 		= new Marketing_Model_Item();
		$tblCategory	= new Marketing_Model_Category();
		$tblUser		= new Default_Model_Users();
		$tblKhoahoc		= new Daotao_Model_Khoahoc();
		
		$this->view->Khoahoc	= $tblKhoahoc->itemInSelectbox(null, array('task' => 'get-khoahoc'));
		$this->view->Users		= $tblUser->listItem(null,array('task' => 'user-all'));
		$this->view->Category	= $tblCategory->itemInSelectbox();
		
		$this->view->Item		= $tblItem->getItem($this->_arrParam, array('task' => 'admin-edit'));
		$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/xml/' . $this->_arrParam['id'] . '.xml';
		if(file_exists($filename)) {
			$xml = simplexml_load_file($filename);
			function simplexml_to_array($xmlobj) {
				$a = array();
				foreach ($xmlobj->children() as $node) {
					if (is_array($node))
						$a[$node->getName()] = simplexml_to_array($node);
					else
						$a[$node->getName()] = (string) $node;
				}
				return $a;
			}
			
			foreach($xml->children() AS $child)
			{
				$arrXml[] = simplexml_to_array($child);
			}
			
			foreach ($arrXml AS $val) {
				$item[$val['id']] = $val['id'];
				$khachhang[$val['id']] = $val;
			}
		} else {
			$item = array();
		}
		$this->view->Khachhang 		= $khachhang;
		$this->view->Item['cid']	= $item;
		
		if($this->_request->isPost()){
			$validator = new Marketing_Form_ValidateItem($this->_arrParam);
			if($validator->isError() == true){
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			}else{
				$arrParam 	= $validator->getData();
				$lastID 	= $tblItem->saveItem($arrParam,array('task'=>'admin-edit'));
				
				$id_xoa = array_diff($item, $this->_arrParam['id_chon']);
				if(!empty($id_xoa)) {
					$this->xoaXmlAction($lastID, $id_xoa);
				}
				
				if(!empty($this->_arrParam['cid'])) {
					$this->view->User = $tblItem->listItem($this->_arrParam, array('task' => 'admin-user'));
					$this->appendXmlAction($lastID, $this->view->User);
				}
				
				if(empty($this->_arrParam['type'])){
					$this->_redirect($this->_actionMain . '/save/ok');
				}else{
					if($this->_arrParam['type'] == 'apply'){
						$this->_redirect($this->_controllerMain . '/edit/id/' . $lastID);
					}
					if($this->_arrParam['type'] == 'new'){
						$this->_redirect($this->_controllerMain . '/add');
					}
				}
			}
		}
	}
	
	public function deleteAction(){
		$this->view->Title = 'Marketing :: Xóa';
		$this->view->headTitle($this->view->Title, true);
		if($this->_request->isPost()){
			if(($this->_arrParam['type'] == 'multi-delete') && ($this->_arrParam['task'] == 'ok')){
				$tblItem = new Marketing_Model_Item();
				$tblItem->deleteItem($this->_arrParam, array('task'=>'admin-delete-muti'));
				$this->_redirect($this->_actionMain . $this->_page . '/save/ok');
			}else
				if(!empty($this->_arrParam['id'])){
				$tblItem = new Marketing_Model_Item();
				$tblItem->deleteItem($this->_arrParam, array('task'=>'admin-delete'));
				$this->_redirect($this->_actionMain . $this->_page . '/save/ok');
			}
		}
	}
	
	public function statusAction(){
		$tblItem = new Marketing_Model_Item();
		$tblItem->changeStatus($this->_arrParam);
		$this->_redirect($this->_actionMain . $this->_page . '/save/ok');
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function sortAction(){
		if($this->_request->isPost()){
			$tblItem = new Marketing_Model_Item();
			$tblItem->sortItem($this->_arrParam, array('task'=>'admin-sort'));
			$this->_redirect($this->_actionMain . $this->_page . '/save/ok');
		}
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function addXmlAction($file_name, $listKhachhang) {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
	
		$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/xml/' . $file_name . '.xml';
	
		$doc = new DOMDocument();
	
		//Tạo
		$doc->version = '1.0';
		$doc->encoding = 'UTF-8';
		$comment_elt = $doc->createComment('Generated by namit@langmaster.edu.vn ');
		$doc->appendChild($comment_elt);
	
		$note_goc = $doc->createElement( 'item' );
		$doc->appendChild($note_goc);
	
		foreach ( $listKhachhang AS $key => $val ) {
			$note_elt = $doc->createElement( 'user' );
			$note_goc->appendChild($note_elt);
				
			$id      			= $doc->createElement( 'id', $val['id'] );
			$name				= $doc->createElement( 'name', $val['name'] );
			$email				= $doc->createElement( 'email', $val['email'] );
			if(!empty($val['phone'])){
				$phone			= $doc->createElement( 'phone', $val['phone'] );
			} else {
				$phone			= $doc->createElement( 'phone', $val['telephone'] );
			}
			$khoahoc			= $doc->createElement( 'khoahoc_name', $val['khoahoc_name'] );
			$trangthai			= $doc->createElement( 'trangthai', '0' );
			$trangthai_email	= $doc->createElement( 'trangthai_email', '0' );
				
			$note_elt->appendChild($id);
			$note_elt->appendChild($name);
			$note_elt->appendChild($email);
			$note_elt->appendChild($phone);
			$note_elt->appendChild($khoahoc);
			$note_elt->appendChild($trangthai);
			$note_elt->appendChild($trangthai_email);
		}
	
		$doc->formatOutput = true;
	
		$doc->saveXML();
		$doc->save($filename);
	
	}
	
	public function appendXmlAction($file_name, $listKhachhang) {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
	
		$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/xml/' . $file_name . '.xml';
		$doc = new DOMDocument();
		
		if(!file_exists($filename)) {
			//Tạo
			$doc->version = '1.0';
			$doc->encoding = 'UTF-8';
			$comment_elt = $doc->createComment('Generated by namit@langmaster.edu.vn ');
			$doc->appendChild($comment_elt);
		
			$note_goc = $doc->createElement( 'item' );
			$doc->appendChild($note_goc);
		
			foreach ( $listKhachhang AS $key => $val ) {
				$note_elt = $doc->createElement( 'user' );
				$note_goc->appendChild($note_elt);
		
				$id      			= $doc->createElement( 'id', $val['id'] );
				$name				= $doc->createElement( 'name', $val['name'] );
				$email				= $doc->createElement( 'email', $val['email'] );
				if(!empty($val['phone'])){
					$phone			= $doc->createElement( 'phone', $val['phone'] );
				} else {
					$phone			= $doc->createElement( 'phone', $val['telephone'] );
				}
				$khoahoc			= $doc->createElement( 'khoahoc_name', $val['khoahoc_name'] );
				$trangthai			= $doc->createElement( 'trangthai', '0' );
				$trangthai_email	= $doc->createElement( 'trangthai_email', '0' );
		
				$note_elt->appendChild($id);
				$note_elt->appendChild($name);
				$note_elt->appendChild($email);
				$note_elt->appendChild($phone);
				$note_elt->appendChild($khoahoc);
				$note_elt->appendChild($trangthai);
				$note_elt->appendChild($trangthai_email);
			}
		
			$doc->formatOutput = true;
		
			$doc->saveXML();
			$doc->save($filename);
		} else {
			$xml = simplexml_load_file($filename);
			
			foreach ( $listKhachhang AS $key => $val ) {
				$id      			= $doc->createElement( 'id', $val['id'] );
				$name				= $doc->createElement( 'name', $val['name'] );
				$email				= $doc->createElement( 'email', $val['email'] );
				if(!empty($val['phone'])){
					$phone			= $doc->createElement( 'phone', $val['phone'] );
				} else {
					$phone			= $doc->createElement( 'phone', $val['telephone'] );
				}
				$khoahoc			= $doc->createElement( 'khoahoc_name', $val['khoahoc_name'] );
				$trangthai			= $doc->createElement( 'trangthai', '0' );
				$trangthai_email	= $doc->createElement( 'trangthai_email', '0' );
				
				$employee = $xml->addChild('user');
				$employee->addChild('id', $val['id']);
				$employee->addChild('name', $val['name']);
				$employee->addChild('email', $val['email']);
				if(!empty($val['phone'])){
					$employee->addChild('phone', $val['phone']);
				} else {
					$employee->addChild('phone', $val['telephone']);
				}
				$employee->addChild('khoahoc', $val['khoahoc_name']);
				$employee->addChild('trangthai', '0');
				$employee->addChild('trangthai_email', '0');
			}
			
			file_put_contents($filename, $xml->asXML());
		}
	}
	
	public function xoaXmlAction($file_name, $id_xoa){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
	
		$filename 	= MODULE_PATH . '/marketing/xml/'.$file_name.'.xml';
		$xml = simplexml_load_file($filename);

		foreach ($id_xoa AS $key => $val) {
			$q 	= "//*[id ='" . $val . "']";
			$target = $xml->xpath($q);
	
			if(!$target)
				return; //Returns null
	
			$domRef = dom_import_simplexml($target[0]); //Select position 0 in XPath array
			$domRef->parentNode->removeChild($domRef);
	
			$dom = new DOMDocument('1.0');
			$dom->preserveWhiteSpace = false;
			$dom->formatOutput = true;
			$dom->loadXML($xml->asXML());
			$dom->save($filename);
		}
	}
	
	public function listXmlAction($file_name) {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/xml/' . $file_name . '.xml';
		if(file_exists($filename)) {
			$xml = simplexml_load_file($filename);
			$item = $xml->xpath("//*");
		} else {
			$item = array();
		}
		return $item;
	}

}



